<template>
  <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="80px" class="login-form">
    <el-form-item label="头像" prop="avatar">
      <IconAvatar v-model:imageUrl="loginForm.avatar"/>
    </el-form-item>
    <el-form-item label="姓名" prop="username">
      <el-input v-model="loginForm.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="loginForm.password"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPassword">
      <el-input type="password" v-model="loginForm.checkPassword"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-radio-group v-model="loginForm.gender">
        <el-radio value="1" size="large">男</el-radio>
        <el-radio value="0" size="large">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="生日" prop="birth">
      <el-date-picker
          v-model="loginForm.birth"
          type="date"
          placeholder="Pick a day"
          :size="'default'"
      />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input
          v-model="loginForm.phone"
          style="width: 240px"
          maxlength="11"
          placeholder="输入中国大陆区手机号码"
          show-word-limit
          type="text"
      />
    </el-form-item>
    <div style="margin-left: 75px">
      <el-form-item>
        <el-button type="primary" @click="submitForm">注册</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>

<script setup>
import {reactive, ref} from 'vue';
import {ElMessage} from 'element-plus';
import Config from "@/stores/config.ts";
import IconAvatar from "@/components/icons/IconAvatar.vue";
import JSEncrypt from 'jsencrypt'
import {RegisterApi} from '@/apis/api.ts';
import router from "@/router/index.ts";

const loginForm = reactive({
  avatar: '',
  username: '',
  password: '',
  checkPassword: '',
  gender: '',
  birth: '',
  phone: '',
});

const loginFormRef = ref(null);

const rules = {
  username: [
    {required: true, message: '请输入姓名', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
  ],
  checkPassword: [
    {required: true, message: '请确认密码', trigger: 'blur'},
    {
      validator: (rule, value, callback) => {
        if (value !== loginForm.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      }
    }
  ],
  gender: [
    {required: true, message: '请选择性别', trigger: 'blur'},
  ],
  birth: [
    {required: true, message: '请填写生日', trigger: 'blur'},
  ],
  phone: [
    {required: true, message: '请输入手机号码', trigger: 'blur'},
  ]
};

const pk = Config.publicKey;

const submitForm = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      // 密码加密
      const form = JSON.parse(JSON.stringify(loginForm));
      const crypt = new JSEncrypt()
      crypt.setPublicKey(pk)
      const encryptedPassword = crypt.encrypt(form.password)
      const encryptedCheckPassword = crypt.encrypt(form.checkPassword)

      form.password = encryptedPassword
      form.checkPassword = encryptedCheckPassword

      // 注册接口
      RegisterApi(form).then((resp) => {
        console.log('注册响应', resp)
        if (resp.code === 200) {
          ElMessage.success('注册提交成功，请等待短信通知');
          // 跳转到登录页
          router.push('/sign-in');
        } else {
          ElMessage.error(resp.message);
        }
      })
    } else {
      ElMessage.error('请检查并修正输入的字段');
    }
  });
};
</script>

<style scoped>
.login-form {
  width: 300px;
  margin-top: 50px;
}
</style>
